package com.jeesite.modules.task.deploy.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.task.action.entity.DcSupTaskAction;
import com.jeesite.modules.task.action.service.DcSupTaskActionService;
import com.jeesite.modules.task.deploy.entity.DcSupTaskInfo;
import com.jeesite.modules.task.deploy.service.DcSupTaskInfoService;
import com.jeesite.modules.task.feedback.entity.DcSupTaskFeedback;
import com.jeesite.modules.task.feedback.service.DcSupTaskFeedbackService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.task.deploy.entity.DcSupTaskInfoAdd;
import com.jeesite.modules.task.deploy.service.DcSupTaskInfoAddService;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 任务部署信息表-记录预期反馈时间Controller
 * @author zrr
 * @version 2022-03-03
 */
@Controller
@RequestMapping(value = "${adminPath}/deploy/dcSupTaskInfoAdd")
public class DcSupTaskInfoAddController extends BaseController {

	@Autowired
	private DcSupTaskInfoAddService dcSupTaskInfoAddService;
	@Autowired
	private DcSupTaskInfoService dcSupTaskInfoService;
	@Autowired
	private DcSupTaskFeedbackService dcSupTaskFeedbackService;
	@Autowired
	private DcSupTaskActionService dcSupTaskActionService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public DcSupTaskInfoAdd get(String id, boolean isNewRecord) {
		return dcSupTaskInfoAddService.get(id, isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("deploy:dcSupTaskInfoAdd:view")
	@RequestMapping(value = {"list", ""})
	public String list(DcSupTaskInfoAdd dcSupTaskInfoAdd, Model model) {
		model.addAttribute("dcSupTaskInfoAdd", dcSupTaskInfoAdd);
		return "modules/task/deploy/dcSupTaskInfoAddList";
	}

	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<DcSupTaskInfoAdd> listData(DcSupTaskInfoAdd dcSupTaskInfoAdd, HttpServletRequest request, HttpServletResponse response) {
		dcSupTaskInfoAdd.setPage(new Page<>(request, response));
		Page<DcSupTaskInfoAdd> page = dcSupTaskInfoAddService.findPage(dcSupTaskInfoAdd);
		List<DcSupTaskInfoAdd> listPage = page.getList();

		DcSupTaskFeedback dcSupTaskFeedback = new DcSupTaskFeedback();
		dcSupTaskFeedback.setActionId(dcSupTaskInfoAdd.getActionId());
		List<DcSupTaskFeedback> list = dcSupTaskFeedbackService.findListOverRide(dcSupTaskFeedback);

		DcSupTaskAction dcSupTaskAction = new DcSupTaskAction();
		dcSupTaskAction.setId(dcSupTaskInfoAdd.getActionId());
		DcSupTaskAction dcSupTaskAction1 = dcSupTaskActionService.get(dcSupTaskAction);

		DcSupTaskInfo dcSupTaskInfo = new DcSupTaskInfo();
		dcSupTaskInfo.setId(dcSupTaskInfoAdd.getTaskId());
		DcSupTaskInfo dcSupTaskInfo1 = dcSupTaskInfoService.get(dcSupTaskInfo);

		for (DcSupTaskInfoAdd a:listPage) {
			for (DcSupTaskFeedback ac:list) {
				if(ac.getFeedbackEndTimeStr().equals(a.getFeedbackEndTimeStr())){
					a.setHisStatus("已反馈");
					if("1".equals(ac.getFeedbackStatus())){
						a.setHisStatus("草稿");
					}
				}
			}
			a.setLeaderOrg(dcSupTaskAction1.getLeaderOrgName());
			a.setTaskTitle(dcSupTaskInfo1.getTaskTitle());
			if(DateCompare(a.getFeedbackEndTime())){
				a.setHisStatus("超时反馈");
			}
		}

		return page;
	}



	public static Boolean DateCompare(Date time) {
		Calendar calendar = Calendar.getInstance();
		int now_y = calendar.get(Calendar.YEAR);//得到年份
		int now_m = calendar.get(Calendar.MONTH) + 1;//得到月份
		int now_d = calendar.get(Calendar.DATE);//得到月份中今天的号数
//	        int now_h = calendar.get(Calendar.HOUR_OF_DAY);//得到一天中现在的时间，24小时制
//	        int now_mm = calendar.get(Calendar.MINUTE);//得到分钟数
//	        int now_s = calendar.get(Calendar.SECOND);//得到秒数
		calendar.setTime(time);
		int y = calendar.get(Calendar.YEAR);//得到年份
		int m = calendar.get(Calendar.MONTH) + 1;//得到月份
		int d = calendar.get(Calendar.DATE);//得到月份中今天的号数
		System.out.println("时间是：" + y + "-" + m + "-" + d);
		if (now_y < y || (now_y == y && now_m < m) || (now_y == y && now_m == m && now_d <= d)) {
		} else {
			return true;
		}

		return false;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("deploy:dcSupTaskInfoAdd:view")
	@RequestMapping(value = "form")
	public String form(DcSupTaskInfoAdd dcSupTaskInfoAdd, Model model) {
		model.addAttribute("dcSupTaskInfoAdd", dcSupTaskInfoAdd);
		return "modules/task/deploy/dcSupTaskInfoAddForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("deploy:dcSupTaskInfoAdd:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated DcSupTaskInfoAdd dcSupTaskInfoAdd) {
		dcSupTaskInfoAddService.save(dcSupTaskInfoAdd);
		return renderResult(Global.TRUE, text("保存任务部署信息-记录预期反馈时间成功！"));
	}

	/**
	 * 删除数据
	 */
	@RequiresPermissions("deploy:dcSupTaskInfoAdd:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(DcSupTaskInfoAdd dcSupTaskInfoAdd) {
		dcSupTaskInfoAddService.delete(dcSupTaskInfoAdd);
		return renderResult(Global.TRUE, text("删除任务部署信息-记录预期反馈时间成功！"));
	}

}